﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class InsertScorePlayers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                sel_tour.DataSource = Tournament.getAvailableTournaments();
                sel_tour.DataTextField = "Tournament_Name";
                sel_tour.DataValueField = "Pk_Tournament";
                sel_tour.DataBind();
                sel_tour.SelectedIndex = 0;
            }

           
            AjaxPro.Utility.RegisterTypeForAjax(typeof(InsertScorePlayers));
        }
        catch (Exception Ex) {
            Response.Write(Ex.ToString());
        } 
        
    }
    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]
    public int InsertScore(int pk_tournament,int p_PlayerNumber,int p_MachineNumber,string p_PlayerScore,bool IsJoker, bool IsQualify)
    {
        try
        {
            //int pk_tournament = int.Parse(sel_tour.SelectedItem.Value);
            List<Player> Players = Player.GetPlayersByTournament(pk_tournament);
            int pk_player = -1;
            foreach (Player iPlayer in Players) {
                if (iPlayer.Player_Number == p_PlayerNumber) {
                    pk_player = iPlayer.Pk_Player;
                    break;
                }
            }
            List<Machine> Machines = Machine.GetMachinesByTournament(pk_tournament);
            int pk_machine = -1;
            foreach (Machine iMachine in Machines)
            {
                if (iMachine.Machine_Number == p_MachineNumber)
                {
                    pk_machine = iMachine.Pk_Machine;
                    break;
                }
            }

            long PlayerScore = long.Parse(p_PlayerScore);

            
            ptss serviceptss = new ptss();
            string error;
            bool res = serviceptss.InsertPlayerScore(pk_tournament, pk_player, pk_machine, PlayerScore, IsQualify, IsJoker, out error);

            if (res == true)
            {
                return 0;
            }
            else {
                return -1;
            }
   
          
            
        }
        catch{
            return -1;
        }
    }
   
}
